package executor.impl;

import com.xiaoming.coupon.vo.CouponTemplateSDK;
import com.xiaoming.coupon.vo.SettlementInfo;
import constant.RuleFlag;
import executor.AbstractExecutor;
import executor.RuleExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 折扣优惠券结算规则执行器
 * @author xiaoming
 * @Date 2020/6/22
 */
@Slf4j
@Component
public class ZheKouExecutor extends AbstractExecutor implements RuleExecutor {
    /**
     * 规则类型标记
     * @return {@link RuleFlag}
     */
    @Override
    public RuleFlag ruleConfig() {
        return RuleFlag.ZHEKOU;
    }

    /**
     * 优惠券规则的计算
     * @param settlementInfo {@link SettlementInfo} 包含了选择的优惠券
     * @return {@link SettlementInfo} 修正过的结算信息
     */
    @Override
    public SettlementInfo computeRule(SettlementInfo settlementInfo) {
        double goodsSum = retain2Decimals(goodsCostSum(settlementInfo.getGoodsInfos()));
        SettlementInfo probability = processGoodsTypeNotSatify(settlementInfo, goodsSum);
        if (null != probability){
            log.debug("ZheKou Template Is Not Match To GoodsType!");
            return probability;
        }

        //折扣优惠券可以直接使用，没有门槛
        CouponTemplateSDK templateSDK =
                settlementInfo.getCouponAndTemplateInfos().get(0).getTemplate();
        double quota = templateSDK.getRule().getDiscount().getQuota();

        //计算使用优惠券之后的价格
        settlementInfo.setCost(
                retain2Decimals((goodsSum * (quota * 1.0 / 100)) > minCost() ?
                        retain2Decimals(goodsSum * (quota * 1.0 /100))
                        : minCost())
        );
        log.debug("Use ZheKou Coupon Make Goods Cost From {} To {}",
                goodsSum, settlementInfo.getCost());

        return settlementInfo;
    }
}
